/******************************************************************************
 *	@Author Raffaello Brondi
 *
 * 	@description Set of utility functions. 
 *				Since this file is meant to be used by all the other scripts 
 * 				it should be always inserted in an html page.
 *
 ******************************************************************************/

function getDomainUrl(){
	return document.location.protocol + '//' + document.location.host;
}
function getRootDomainUrl(){
//	return getDomainUrl() + '/sonna/';
	return getDomainUrl();
}

function getRestUrl(){
	return getRootDomainUrl() + '/rest/';
}

function fbGetPhotosUrl(uid){
	return 'https://graph.facebook.com/' + uid + '/albums?fields=photos';
}

function fbGetVideosUrl(uid){
	return 'https://graph.facebook.com/' + uid + '/videos/uploaded?fields=created_time,description,from,name,source,updated_time,comments.fields(from,id,message,like_count,created_time,comments,likes),likes.fields(id,name,username)';
}

function getFBtnsContainerId(){
	return 'fb_btns_container';
}

function getFBLogoutBtnId(){
	return 'fb_logout_btn';
}

/*******************************************************************************
 * @description Returns the HTML head tag if present, undefined otherwise.
 * @return Returns the HTML head tag if present otherwise it returns nothing
 ******************************************************************************/
function getHeadTag(){
	var head = document.getElementsByName('head');
	if(head.length > 0)
		return head[0];
	return undefined;
}

/*******************************************************************************
 * @description Returns the first HTML script tag if present.
 * @return Returns the first HTML script tag if present,
 * 			otherwise it returns nothing
 ******************************************************************************/
function getScriptTag(){
	var scripts = document.getElementsByTagName( 'script' );
	
	if(scripts.length > 0)
		return scripts[ scripts.length - 1 ];
	return undefined;
}

/*******************************************************************************
 * @description Creates the HTML head tag and returns it.
 * @return Returns the HTML head tag.
 ******************************************************************************/
function createHeadTag(){
	var head = document.createElement('head');
	var root = document.getElementsByTagName('html')[0];
	root.appendChild(head);
	return head;
}

/*******************************************************************************
 * @description Creates a new script element
 * @param The location where the script can be found.
 * @param Wheter or not the script has to be loaded asynchronously or not.
 * @param Function to be called when the script has been loaded.
 * @return Returns the HTML head tag.
 ******************************************************************************/
function newScriptElement(src_loc, async, onComplete) {
	var script = document.createElement('script');
	
	script.src = src_loc;
	if(async != null)
		script.async = async;
	script.setAttribute("type", "text/javascript");

	if(onComplete){
		script.onload = onComplete;
		script.onreadystatechange = function(){
			if (this.readyState == 'complete') onComplete();
		}
	}
	return script;
}

/*******************************************************************************
 * @description Creates a new <li> element and sets its content to data
 * @param data The html content to put inside the element
 * @return Returns the new <li> element
 ******************************************************************************/
function newLIElement(data) {
	var elem = document.createElement('li');
	elem.innerHTML = data;
	return elem;
}

/*******************************************************************************
 * @description Checks if it is IE
 * @return Returns true if the browser used is IE, false otherwise.
 ******************************************************************************/
function isInternetExplorer(){
  return navigator.appName == 'Microsoft Internet Explorer';
}


/*******************************************************************************
 * @description Replacer for JSON Srtingify
 * @param key ??
 * @param value ??
 * @return Returns true if the browser used is IE, false otherwise.
 ******************************************************************************/
 function replacer(key, value) {
    //if(!typeof value === "object")// && value.contains('&'))
	if(value.replace)
        //return value.replace('&', '&amp;');
		return value.replace(/&/g, '%26');
    return value;
}

/*******************************************************************************
 * @description Prints a log message. Using IE this results in showing an alert.
 *				If the browser used has a cosole the message is printed there,
 *				otherwise an alert is shown
 * @param msg The message to log.
 ******************************************************************************/
function log(msg){
	if(isInternetExplorer())
		alert(msg);
	else if(console == undefined || !console)
		alert(msg);
	else
		console.log(msg);
}
